GBML manual / Grids

grid_shuffle_rows

Mixes up the order of the rows of target, in place. Each row moves as a whole, so the cells of a row stay together and in the same order left to right; only which row comes first, second and so on changes. It makes no copy.

Syntax

grid_shuffle_rows(target)

Arguments

NameType
targetgrid

Returns

Nothing.

Description

Mixes up the order of the rows of target, a grid. Each row moves as a whole, so the cells of a row stay together and stay in the same order from left to right. Only which row comes first, second, third and so on changes.

Before          After grid_shuffle_rows(table)
1  50           3  70
2  90           1  50
3  70           2  90

That is what to use for a table where each row means something as a whole, like a player's number and their score: the scores stay beside the right players. To mix up every cell on its own instead, use grid_shuffle. To move whole columns, use grid_shuffle_columns.

It changes the grid itself

grid_shuffle_rows moves the values that are already in target. It does not make a new grid, and it needs no extra memory. The old order of the rows is gone afterwards. If you want to keep it, make a copy with grid_copy first. The grid keeps its width and height.

Any kind of value can be shuffled: numbers, text, structs, references. Nothing is compared, so there is no rule about what the grid holds.

Where the random numbers come from

A shuffle picks its random numbers from the same place roll_int does: one stream of random numbers for the whole game. There is no seed to give the shuffle itself. What follows from that:

Every possible order of the rows can come up. It is very slightly more likely to land on some than others, because a random number is cut down to fit a small range, but the difference is far too small for a game to show.

The grid has to know its size

To find a row, GBML needs to know how many cells are in it. A grid only knows its width and height where it was made (grid_create(8, 6, 0), /// @capacity 8x6, or grid<int, 8, 6>). A grid with no size written like that, or a script's grid parameter, has none to give, and the call is refused, the same as grid_width is. In a script, grid_shuffle still works on the whole grid.

What happens in the awkward cases

You do thisWhat happens
shuffle a grid with one rownothing changes, and no random numbers are used
shuffle rows that are the same as each otherworks as normal. You cannot tell which is which
shuffle and hope a row movesit may stay where it was. That can happen with random order
give it a list or an arrayrefused when checked
use it where a value is neededrefused when checked. It gives nothing back

What it costs

Nothing is set aside: the rows are swapped where they stand, one row swap (a swap of every cell in the two rows) for each row after the first. No timing was measured.

Example

var table = grid_create(2, 3, 0);
table[# 0, 0] = 1;
table[# 1, 0] = 50;
table[# 0, 1] = 2;
table[# 1, 1] = 90;
table[# 0, 2] = 3;
table[# 1, 2] = 70;
grid_shuffle_rows(table);    // the three rows in a new order, each row unchanged

On the GBA

A Fisher-Yates shuffle over the row numbers, where swapping two rows swaps their width cells one by one. A Fisher-Yates shuffle done in place: it makes no copy and needs no extra memory. For each place from the last down to the second, it swaps that place with one picked at random from the places up to and including it, so every order can come up. The pick comes from gbml::roll_int, the one stream the roll_ functions use, so a shuffle follows the same rules: the same ROM shuffles the same way every time it is switched on, roll_reseed changes that, and roll_reseed with the same number just before a shuffle gives the same shuffle again. A shuffle uses up rolls from that stream (one for each row after the first), so it moves every later roll along. Nothing is compared, so any kind of item can be shuffled. It needs the grid's width and height, which the checker reads from the declaration and writes into the C++ as plain numbers; a grid with none, such as a script's parameter, is refused.

See also: grid_shufflegrid_shuffle_columnsgrid_sortlist_shufflegrid_widthgrid_heightgrid_copyroll_introll_reseedGridsData containers